<?
require("header.inc.php");

require("classes.inc.php");
$login = new loginmanager;
$login->verify();

if ($_SESSION['userlevel'] == 1)
{ 
  $godmode = true; 
}
else
{ 
  $godmode = false;
}

if($godmode) echo "<script type=\"text/javascript\">
function shownewproj()
{
  var length = document.forms['addhoursform'].projects.options.length
  if (document.forms['addhoursform'].projects.options.selectedIndex == length - 1)
  {
    unhide('newproject');
  }
  else
  {
    hide('newproject');
  }
}

function shownewcode()
{
  var length = document.forms['addhoursform'].kostencodes.options.length
  if (document.forms['addhoursform'].kostencodes.options.selectedIndex == length - 1)
  {
    unhide('newcode');
  }
  else
  {
    hide('newcode');
  }
}

function hide(id)
{
  document.getElementById(id).style.display = \"none\";
}	
function unhide(id)
{
  document.getElementById(id).style.display = \"\";
}</script>";

$db = new database_connection();
$db->connect("localhost", "root", "", "roconsult");

if(isset($_POST['addhours']))     #User probeert code toe te voegen.
{ 
  echo "KOMT NOG!<br />\n";
  echo "Employeenr: ".$_SESSION['empnr']."<br />\n";
  if ($_POST['projects'] != -1){
    echo "Projnr: ".$_POST['projects']."<br >\n";     #bestaand projectnr
  }else{
    echo "Projnr: ".$_POST['newproj']."<br />\n";     #nieuw projectnr aanvragen
    $db->query("insert into `projecten` (`projectnaam`) values ('".escape($_POST['newproj'])."');");
    $db->query("select `projectnr` from `projecten` where `projectnaam` = '".escape($_POST['newproj'])."'");
    $temp = $db->output();
    $_POST['projects'] = $temp['projectnr'];
  }
  if ($_POST['kostencodes'] != -1){
    echo "KostenCode: ".$_POST['kostencodes']."<br />\n";
    echo "New KostenCode: ".$_POST['newkostencode']."<br />\n";
  }else{
    echo "KostenCode: ".$_POST['newkostencode']."<br />\n";
    $db->query("insert into `kosten` (`Kostenomschrijving`) values ('".escape($_POST['newkostencode'])."');");
    $db->query("select `kostencode` from `kosten` where `kostenomschrijving` = '".escape($_POST['newkostencode'])."' LIMIT 0,1;");
    $temp = $db->output();
    $_POST['kostencodes'] = $temp['kostencode'];
  }
  
  $db->query("insert into `koppeling` (`empnr`, `projectnr`, `kostencode`, `datum`, `bedrag`) values('".$_SESSION['empnr']."', '".escape($_POST['projects'])."', '".escape($_POST['kostencodes'])."', NOW(), '".escape($_POST['newbedrag'])."' ) ");
  $db->closelink();
  header("Location: index.php");
}
else                              # User wilt het voeg toe scherm zien.
{ 
  $db = new database_connection();
  $db->connect("localhost", "root", "", "roconsult");
  $db->query("select `projectnr`, `projectnaam` from `projecten`");
    
  echo "<form action=\"#\"method=\"post\" name=\"addhoursform\">\n";
  echo "<table border=\"1\" style=\"border-collapse:collapse;\">";
  echo "<tr><td colspan=\"2\" align=\"center\">Uren Formulier</td></tr>";
  
  // Projects Select Box
  echo "<tr><td>Project:</td><td><select name=\"projects\" onchange=\"shownewproj();\">\n";
  while ($option = $db->output())
  {
    echo "\t<option value=\"".$option['projectnr']."\">".$option['projectnaam']."</option>\n";
  }
  if($godmode) echo "\t<option value=\"-1\">Nieuw Project</option>\n";
  echo "</select></td></tr>";  
  if($godmode) echo "<tr id=\"newproject\" style=\"display:none;\"><td>Nieuw Project:</td><td><input type=\"text\" name=\"newproj\" /></td></tr>";
  // Einde Projects Select Box
  
  //KostenCode Select Box
  $db->query("select `kostencode`, `kostenomschrijving` from `kosten`");
  echo "<tr><td>KostenCode</td><td><select name=\"kostencodes\" onchange=\"shownewcode();\">";
  while ($option = $db->output())
  {
    echo "\t<option value=\"".$option['kostencode']."\">".$option['kostenomschrijving']."</option>\n";
  }
  if($godmode) echo "\t<option value=\"-1\">Nieuwe KostenCode</option>\n";
  echo "</select></td></tr>";
  if($godmode) echo "<tr id=\"newcode\" style=\"display:none;\"><td>Nieuwe KostenCode:</td><td><input type=\"text\" name=\"newkostencode\" /><br /></td></tr>";
  echo "<tr><td>Bedrag:</td><td><input type=\"text\" name=\"newbedrag\" /></td></tr>";
  //Einde KostenCode Select Box
  
  echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"Verstuur\" name=\"addhours\" /></td></tr>";
  echo "</table>";
  echo "</form>\n";
  
  $db->closelink();
}

require("footer.inc.php");
?>